//
//  SharpObstacle.m
//  StarBugs
//
//  Created by ?? ? on 3/22/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/SharpObstacle.h"
#include "Util/ImageManager.h"

using namespace cocos2d;


SharpObstacle::SharpObstacle()
{
    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    
    frame[0] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "awl_1");
    frame[0]->retain();
    
    frame[1] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "awl_2");
    frame[1]->retain();
    
    frame[2] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "awl_3");
    frame[2]->retain();
    
    frame[3] = ImageManager::getFrameWithXml2(kImagesheet_GameSource, "awl_4");
    frame[3]->retain();
    
    mainSprite->setDisplayFrame(frame[0]);
    objectType = objectType_sharp;
    
    setAniTime(2.0, 3.0);
}

void SharpObstacle::setBody(b2World *world)
{
    mainSprite->setPosition(position);
    mainSprite->setRotation(bodyAngle);
}


void SharpObstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    mainSprite->setRotation(bodyAngle);
}

void SharpObstacle::setAniTime(float apear, float disapear)
{
    unscheduleAllSelectors();
    apearTime = apear;
    disapearTime = disapear;
    currentTime = 0;
    cnt = 0;
    schedule(schedule_selector(SharpObstacle::doAni));
    apearFlg = true;
}

void SharpObstacle::doAni(float dt)
{
    currentTime += dt;
    if(currentTime > disapearTime && apearFlg == true)
    {
        cnt = 0;
        schedule(schedule_selector(SharpObstacle::doApear), 0.1);
        apearFlg = false;
        currentTime = 0;
    }   
    else if(currentTime > apearTime && apearFlg == false)
    {
        cnt = 0;
        schedule(schedule_selector(SharpObstacle::doDisapear), 0.1);
        apearFlg  = true;
        currentTime = 0;
    }
}

void SharpObstacle::doApear(float dt)
{
    cnt ++;
    mainSprite->setDisplayFrame(frame[cnt]);
    if(cnt == 3)
    {
        unschedule(schedule_selector(SharpObstacle::doApear));
    }
}

void SharpObstacle::doDisapear(float dt)
{
    cnt ++;
    mainSprite->setDisplayFrame(frame[3 - cnt]);
    if(cnt == 3)
    {
        unschedule(schedule_selector(SharpObstacle::doDisapear));
    }
}

void SharpObstacle::resetObstacle()
{
    setAniTime(2.0, 3.0);
}
